package com.realwronger.demo;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Neo
 * 
 */
public class GenericDemo {

	static class A {
		public A(String name) {
			this.name = name;
		}

		public String name;

		@Override
		public String toString() {
			return "A: " + name;
		}
	}

	public static Collection<A> createCollectionA() {
		Collection<A> col = new ArrayList<A>();
		col.add(new A("Neo"));
		col.add(new A("Kelly"));
		return col;
	}

	// wildcard types
	public static void printCollection(Collection<?> col) {
		for (Object e : col) {
			System.out.println(e);
		}
	}

	public void UseWildcardType() {
		Collection<A> col = createCollectionA();
		printCollection(col);
	}

	// generic method
	public static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
		for (T o : a) {
			c.add(o);
		}
	}

	public void UseGenericMethod() {
		A[] a = new A[] { new A("Neo"), new A("Kelly") };
		ArrayList<A> c = new ArrayList<A>();
		fromArrayToCollection(a, c);
		printCollection(c);
	}

	public static void main(String[] args) {
		GenericDemo demo = new GenericDemo();
		//demo.UseWildcardType();
		demo.UseGenericMethod();
	}
}
